using System;

namespace Solver
{
	public static partial class NumberExtensions
	{
		public static int SumOfDivisors(this int n, int[] primes)
		{
			var maxPrime = primes[primes.Length - 1];
			if (maxPrime * maxPrime < n) throw new ArgumentException("Not enough primes!");
			if (n == 0) return 0;
			int s = 1;
			int nn = n;
			foreach (var p in primes)
			{
				if (p * p > n) break;
				if (n % p != 0) continue;
				var t = p;
				while (n % p == 0)
				{
					n /= p;
					t *= p;
				}
				s *= (t - 1) / (p - 1);
			}
			if (n > 1) s *= n + 1;
			return s - nn;
		}
	}
}